<template>
  <div>
    <el-dialog
      title="评价详情"
      :visible="evaDetailVisible"
      :close-on-click-modal="false"
      @close="evaCloseDialog"
      width="900px"
      :modal-append-to-body="false"
      :close-on-press-escape="false"
      class="dialogForm"
      :append-to-body="true"
    >
      <ReduceDialog
        :scoreDetailVisible="scoreDetailVisible"
        :reduceScoreDetail="reduceScoreDetail"
        @closeDialog="closeDialog"
        v-if="isShowScore"
      ></ReduceDialog>
      <div class="pointBox">
        <el-row style="font-weight: 560; color: #333333">
          存在
          <span style="color: #e02020">{{ total }}</span> 项扣分
        </el-row>
        <div class="noDeduction" v-if="negetivePoints.length < 1">
          <img src="../../../static/img/noData.png" alt />
          <div class="text fS color999">暂无扣分项</div>
        </div>
        <!-- 扣分项 -->
        <div v-if="negetivePoints.length > 0">
          <div class="deduction" v-for="(item, i) in negetivePoints" :key="i">
            <p class="dedTitle fS">
              <span class="name">{{ item.name }}</span>
              <span class="count"
                >(根据项目数据，存在{{ item.count }}项扣分)</span
              >
            </p>
            <div>
              <ul>
                <li
                  class="mrT12"
                  v-for="(itemChild, j) in item.indexs"
                  :key="j"
                >
                  <div
                    class="clearfix"
                    @click="goRepair(i, j, itemChild.id)"
                    style="position: relative"
                  >
                    <i
                      class="icon iconfont iconzhankai"
                      style="
                        color: #e02020;
                        font-size: 12px;
                        position: absolute;
                        top: 7px;
                      "
                      v-if="isShowDetail !== i + '/' + j"
                    ></i>
                    <i
                      class="icon iconfont iconshouqi"
                      style="
                        color: #e02020;
                        font-size: 12px;
                        position: absolute;
                        top: 7px;
                      "
                      v-if="isShowDetail === i + '/' + j"
                    ></i>
                    <span
                      class="dedCon fS ellipsis"
                      style="width: 60%; margin-left: 20px"
                      :title="
                        itemChild.reason
                          ? itemChild.reason.indexOf('2、扣分明细') > -1
                            ? itemChild.reason
                                .split('2、扣分明细')[0]
                                .split('扣分原因：')[1]
                            : itemChild.reason.indexOf('扣分原因：') > -1
                            ? itemChild.reason.split('扣分原因：')[1]
                            : itemChild.reason
                          : '--'
                      "
                      >{{
                        itemChild.reason
                          ? itemChild.reason.indexOf('2、扣分明细') > -1
                            ? itemChild.reason
                                .split('2、扣分明细')[0]
                                .split('扣分原因：')[1]
                            : itemChild.reason.indexOf('扣分原因：') > -1
                            ? itemChild.reason.split('扣分原因：')[1]
                            : itemChild.reason
                          : '--'
                      }}</span
                    >
                    <el-button
                      size="small"
                      class="funBut fr"
                      v-if="isShowDetail !== i + '/' + j"
                      >修复</el-button
                    >
                    <el-button
                      size="small"
                      class="funBut fr"
                      v-if="isShowDetail === i + '/' + j"
                      >{{ isOpenText }}</el-button
                    >
                    <span class="dedCon fr fS" v-if="!itemChild.gradeLevel"
                      >-{{ itemChild.score }}分</span
                    >
                    <span class="dedCon fr fS" v-if="itemChild.gradeLevel"
                      >定级{{
                        itemChild.gradeLevel
                          ? itemChild.gradeLevel === 'O'
                            ? '其他'
                            : itemChild.gradeLevel
                          : '--'
                      }}</span
                    >
                  </div>
                  <el-collapse-transition>
                    <div class="rankDetail" v-if="isShowDetail === i + '/' + j">
                      <div>
                        <div class="clearfix">
                          <p class="conTitle fl">
                            {{ specificData.indexName }}
                          </p>
                          <p
                            class="fr pointsLevel"
                            v-if="specificData.gradeLevel"
                          >
                            定级{{
                              specificData.gradeLevel
                                ? specificData.gradeLevel === 'O'
                                  ? '其他'
                                  : specificData.gradeLevel
                                : '--'
                            }}
                          </p>
                          <p
                            class="fr pointsLevel"
                            v-if="!specificData.gradeLevel"
                          >
                            -{{ specificData.score }}分
                          </p>
                        </div>
                        <p class="labelWidth clearfix">
                          <span class="fS fl">扣分原因：</span>
                          <span class="fS fl w80">
                            {{
                              specificData.reason
                                ? specificData.reason.indexOf('2、扣分明细') >
                                  -1
                                  ? specificData.reason
                                      .split('2、扣分明细')[0]
                                      .split('扣分原因：')[1]
                                  : specificData.reason.indexOf('扣分原因：') >
                                    -1
                                  ? specificData.reason.split('扣分原因：')[1]
                                  : specificData.reason
                                : '--'
                            }}
                            <span
                              v-if="
                                specificData.reason &&
                                specificData.reason.indexOf('2、扣分明细') > -1
                              "
                              @click="showScoreDetail(itemChild.id)"
                              class="reduceDetail"
                              >扣分明细</span
                            >
                          </span>
                        </p>
                        <p class="labelWidth clearfix">
                          <span class="fS fl">责任主体：</span>
                          <span class="fS fl w80">
                            <span class="type">{{
                              specificData.typeDesc
                            }}</span>
                            {{ specificData.companyName }}
                          </span>
                        </p>
                        <p class="labelWidth clearfix">
                          <span class="fS fl">评价标准：</span>
                          <span class="fS fl w80">{{
                            specificData.evaluateStandard
                          }}</span>
                        </p>
                        <p class="labelWidth clearfix">
                          <span class="fS fl">参考条例：</span>
                          <span class="fS fl w80">{{
                            specificData.referenceRules
                          }}</span>
                        </p>
                      </div>
                      <div class="way">
                        <p class="fS repair">如何修复</p>
                        <!-- <div
                                                        v-html="specificData.repairTips"
                                                        class="howRepair"
                                                    ></div>-->
                        <p class="labelWidth clearfix">
                          <span class="fS fl">修复方式：</span>
                          <span class="fS fl w80">{{
                            specificData.repairTips
                          }}</span>
                        </p>
                      </div>
                    </div>
                  </el-collapse-transition>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import common from '../../kits/common'
import ReduceDialog from './ReduceDialog'

export default {
  name: 'evaluationDetailsDialog',
  props: {
    evaDetailVisible: {
      type: Boolean,
      default: false,
    },
    detailRow: {
      type: Object,
      default: false,
    },
  },
  components: {
    ReduceDialog,
  },
  data() {
    return {
      total: '',
      project: {},
      companyProjectScore: {},
      negetivePoints: [],
      isShowDetail: '',
      isOpenText: '', //扣分
      reduceScoreDetail: {
        detailId: '',
        type: '',
        reduceScoreDetailInfo: [],
      },
      scoreDetailVisible: false, //扣分明细弹窗
      isShowScore: false,
    }
  },
  created() {
    let { id, projectId, type, uscc } = this.detailRow
    this.getEnterpriseDetail(id, projectId, type, uscc)
  },
  methods: {
    evaCloseDialog() {
      this.$emit('evaCloseDialog')
    },
    getEnterpriseDetail(companyProjectScoreId, projectId, type, uscc) {
      this.axios({
        method: 'post',
        url: 'api-ps/creditComprojScorDel/expand/getCompanyProjectScoreDetail',
        data: {
          companyProjectScoreId,
          type,
          projectId,
          uscc,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            let results = response.data.result
            this.total = results.totalCount
            this.project = results.project
            this.companyProjectScore = results.companyProjectScore
            this.negetivePoints = results.indexDimList
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 扣分点击显示详情
    async goRepair(index, j, id) {
      this.reduceScoreDetail.detailId = id
      if (this.isShowDetail === index + '/' + j) {
        this.isShowDetail = -1 //在这里就将它赋值为-1  由于：-1 !=  index 所以其他内容隐藏 ，被点击的则打开
      } else {
        await this.getDetailData(id)
        this.isShowDetail = index + '/' + j //这里是把index赋值给isShow，isShow=index则显示
        this.isOpenText = '收起'
        this.isShowBonusPoints = -1
      }
    },
    getDetailData(id) {
      return this.axios({
        method: 'post',
        url: 'api-ps/creditComprojScorDel/expand/getCompanyProjectScoreDetailById',
        data: {
          id,
        },
      })
        .then((resp) => {
          if (!resp.data.hasError) {
            this.specificData = resp.data.result
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 扣分明细弹窗
    showScoreDetail(detailId) {
      return this.axios({
        method: 'post',
        url: 'api-ps/creditDeductionDetail/expand/getCreditDedDetail',
        data: {
          detailId,
        },
      })
        .then((response) => {
          if (response.data.hasError) {
            this.$message.error(response.data.errorMsg)
          } else {
            const results = response.data.result
            this.reduceScoreDetail.type = results.type
            if (results.type == '1') {
              this.handleData(results, 'names')
            } else if (results.type == '2') {
              this.handleData(results, 'recordTimes')
            } else {
              this.handleData(results, 'timeAndName')
            }
            this.scoreDetailVisible = true
            this.isShowScore = true
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 处理明细数据
    handleData(results, detailName) {
      if (
        this.specificData.reason &&
        this.specificData.reason.indexOf('扣分明细：') > -1
      ) {
        let reduceStr = this.specificData.reason.split('扣分明细：')[1]
        if (reduceStr.indexOf('b') > -1) {
          // 存在未代发或补发或两条明细（A为补发）
          let reduceReason = this.specificData.reason
            .split('扣分明细：')[0]
            .split('扣分原因：')[1]
          let detailInfo = {}
          if (reduceReason.split('/')[0].indexOf('存在0') === -1) {
            detailInfo.title = reduceStr.split('b')[0].split('a')[1]
            let notIssued = null
            notIssued = results[detailName].filter((v) => {
              if (v.indexOf('A') === -1) {
                return v
              }
            })
            detailInfo.detail = notIssued
            this.reduceScoreDetail.reduceScoreDetailInfo.push(
              JSON.parse(JSON.stringify(detailInfo))
            )
          }
          if (reduceReason.split('/')[1].indexOf('存在0') === -1) {
            detailInfo = {}
            detailInfo.title = reduceStr.split('b')[1]
            let reissue = []
            results[detailName].forEach((v) => {
              if (v.indexOf('A') > -1) {
                reissue.push(v.split('A')[1])
              }
            })
            detailInfo.detail = reissue
            this.reduceScoreDetail.reduceScoreDetailInfo.push(
              JSON.parse(JSON.stringify(detailInfo))
            )
          }
        } else {
          this.reduceScoreDetail.reduceScoreDetailInfo[0] = {}
          this.reduceScoreDetail.reduceScoreDetailInfo[0].title = reduceStr
          if (detailName === 'timeAndName') {
            let detailArr = []
            let detailNameArr = Object.keys(results[detailName])
            for (let i = 0; i < detailNameArr.length; i++) {
              detailArr[i] = {}
              detailArr[i].month = detailNameArr[i]
              detailArr[i].name = JSON.parse(
                JSON.stringify(results[detailName][detailNameArr[i]])
              )
            }
            this.reduceScoreDetail.reduceScoreDetailInfo[0].detail = JSON.parse(
              JSON.stringify(detailArr)
            )
          } else {
            this.reduceScoreDetail.reduceScoreDetailInfo[0].detail = JSON.parse(
              JSON.stringify(results[detailName])
            )
          }
        }
      }
    },
    closeDialog() {
      this.scoreDetailVisible = false
      this.isShowScore = false
      this.reduceScoreDetail.reduceScoreDetailInfo = []
    },
  },
}
</script>

<style scoped lang="less">
.pointBox {
  background: #fff;
  padding: 10px 22px;
  /deep/.el-divider__text.is-left {
    left: 0;
  }
  /deep/.el-divider__text {
    padding: 0;
    padding-right: 12px;
    font-size: 16px;
    color: #333333;
  }
  .deduction {
    margin-top: 26px;
    > div {
      padding-left: 96px;
    }
    .dedTitle {
      margin-bottom: 6px;
      .name {
        font-size: 14px;
        color: #333333;
        font-weight: 560;
      }
      .count {
        font-size: 14px;
        color: #333333;
      }
    }
    .funBut {
      color: #009588;
      border-color: #009588;
      background: #fff;
      width: 96px;
      margin-left: 60px;
    }
    .dedCon {
      display: inline-block;
      margin-top: 5px;
      font-size: 14px;
      color: #3a3a3a;
    }
    .rankDetail {
      margin-top: 12px;
      background: #f5f7fa;
      padding: 22px 24px;
      .conTitle {
        margin-bottom: 20px;
        font-size: 16px;
        color: #333333;
        font-weight: 560;
      }
      .way {
        margin-top: 12px;
        /deep/.howRepair {
          span {
            font-size: 14px !important;
            color: #717171 !important;
            font-family: 'Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Arial,sans-serif' !important;
          }
        }
      }
      .pointsLevel {
        font-size: 24px;
        color: #e83024;
      }
      .type {
        font-size: 12px;
        border: 1px solid #009588;
        padding: 0 2px;
        color: #009588;
        border-radius: 2px;
      }
    }
  }
  .noDeduction {
    text-align: center;
    margin-top: 48px;
    img {
      width: 160px;
      margin-bottom: 15px;
    }
  }
}
.reduceDetail {
  color: rgb(64, 158, 255);
  margin-left: 10px;
  text-decoration: underline;
  cursor: pointer;
}
.mrT12 {
  margin-top: 12px;
}
</style>
